package com.sx.controller;

import com.sx.basic.pageable.PageRequest;
import com.sx.basic.ResponseEntity;
import com.sx.entity.basic.ArticleEntity;
import com.sx.entity.basic.CategoryEntity;
import com.sx.entity.basic.FileEntity;
import com.sx.mapping.BasicMapping;
import com.sx.service.basic.IArticleService;
import com.sx.service.basic.ICategoryService;
import com.sx.service.basic.IFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author AdinZ
 * @date 2020/11/12 14:32
 *
 */
@RestController @Api("基础功能接口")
public class BasicController {
    @Resource private ICategoryService categoryService;
    @Resource private IArticleService articleService;
    @Resource private IFileService fileService;




    @GetMapping(BasicMapping.CATEGORY)@ApiOperation("文章分类列表")
    public ResponseEntity<List<CategoryEntity>> pageCategory(@RequestParam("page")int page,
                                                             @RequestParam("size")int size){
        return this.categoryService.findPage(PageRequest.of(page,size, Sort.Direction.DESC,"sort"));
    }

    @GetMapping(BasicMapping.CATEGORY_GET)@ApiOperation("获取一个文章分类")
    public ResponseEntity<CategoryEntity> getCategory(@PathVariable("id")Long id){
        if(id==null)return new ResponseEntity<>().toError();
        return new ResponseEntity<>().toSuccess(this.categoryService.get(id));
    }

    @PutMapping(BasicMapping.CATEGORY)@ApiOperation("添加修改文章分类")
    public ResponseEntity putCategory(@RequestBody CategoryEntity entity){
        if(entity==null)return new ResponseEntity<>().toError();
        if(entity.getId()==null){
            this.categoryService.save(entity);
        }else{
            this.categoryService.update(entity);
        }
        return new ResponseEntity<>().toSuccess();
    }

    @DeleteMapping(BasicMapping.CATEGORY)@ApiOperation("删除文章分类")
    public ResponseEntity deleteCategory(@RequestParam("id")Long[] ids){
        if(ids==null||ids.length==0)return new ResponseEntity().toError();
        this.categoryService.delete(ids);
        return new ResponseEntity().toSuccess();
    }




    @GetMapping(BasicMapping.ARTICLE)@ApiOperation("文章列表")
    public ResponseEntity<List<ArticleEntity>> pageArticle(@RequestParam("page")int page,
                                                             @RequestParam("size")int size){
        return this.articleService.findPage(PageRequest.of(page,size, Sort.Direction.DESC,"sort"));
    }

    @GetMapping(BasicMapping.ARTICLE_GET)@ApiOperation("获取一个文章")
    public ResponseEntity<ArticleEntity> getArticle(@PathVariable("id")Long id){
        if(id==null)return new ResponseEntity<>().toError();
        return new ResponseEntity<>().toSuccess(this.articleService.get(id));
    }

    @PutMapping(BasicMapping.ARTICLE)@ApiOperation("添加修改文章")
    public ResponseEntity putArticle(@RequestBody ArticleEntity entity){
        if(entity==null)return new ResponseEntity<>().toError();
        if(entity.getId()==null){
            this.articleService.save(entity);
        }else{
            this.articleService.update(entity);
        }
        return new ResponseEntity<>().toSuccess();
    }

    @DeleteMapping(BasicMapping.ARTICLE)@ApiOperation("删除文章")
    public ResponseEntity deleteArticle(@RequestParam("id")Long[] ids){
        if(ids==null||ids.length==0)return new ResponseEntity().toError();
        this.articleService.delete(ids);
        return new ResponseEntity().toSuccess();
    }

    @PostMapping(BasicMapping.UPLOAD_FILE) @ApiOperation("文件上传")
    public ResponseEntity<String> uploadFile(@RequestParam(value = "file",required = false) MultipartFile file){
        try {
            String url = this.fileService.upload(file);
            return new ResponseEntity<>().toSuccess(url);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<>().toError(e.getMessage());
        }

    }

    @GetMapping(BasicMapping.UPLOAD_FILE)@ApiOperation("文件列表")
    public ResponseEntity<List<FileEntity>> pageFile(@RequestParam("page")int page,
                                                     @RequestParam("size")int size){
        return this.fileService.findPage(PageRequest.of(page,size, Sort.Direction.DESC,"id"));
    }

    @DeleteMapping(BasicMapping.UPLOAD_FILE)@ApiOperation("删除文件")
    public ResponseEntity deleteFile(@RequestParam("id")Long[] ids){
        if(ids==null||ids.length==0)return new ResponseEntity().toError();
        this.fileService.delete(ids);
        return new ResponseEntity().toSuccess();
    }


}
